#!/bin/sh
#
# @author Markus Raab <elektra@markus-raab.org>
# @brief Creates a plugin out of the template plugin
# @date 05.05.2012
# @tags generator, creator

set -e

if [ $# -ne 1 ]; then
	echo "Usage: $0 <new-pluginname>"
	echo
	echo "allows you to copy templates and rename them properly"
	echo "This was meant for creating new plugins"
	exit 0
fi

PLUGIN=$1

if echo $PLUGIN | egrep -q '^[a-z]+$'; then
	if [ -d $PLUGIN ]
	then
		echo "Plugin $PLUGIN already exists"
		exit 1
	else
		echo "Creating new plugin $PLUGIN"
	fi
else
	echo "Pluginnames must consist only of characters a-z"
	exit 1
fi

First=`echo $PLUGIN | sed 's/\<./\u&/'`
ALL=`echo $PLUGIN | sed 's/.*/\U&/'`

cp -r template $PLUGIN
cd $PLUGIN
mv template.c $PLUGIN.c
mv template.h $PLUGIN.h
mv testmod_template.c testmod_$PLUGIN.c

do_replacements()
{
	sed -i s/TEMPLATE/$ALL/g $*
	sed -i s/Template/$First/g $*
	sed -i s/template/$PLUGIN/g $*
}

do_replacements testmod_$1.c $1.c $1.h CMakeLists.txt README.md
